<?php
include '../includes/cDatabase.php';
include '../includes/cPlayer.php';
include '../includes/variables.php';
include '../includes/cPage.php';
include '../includes/cDates.php';

$cPage = new page("Show Active Players");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

define("TD_NAME", "<td width='800' align='center'>");
define("TD_MAINNAME", "<td width='200' align='center'>");
define("TD_PLAYER", "<td width='200' align='center'>");
define("TD_NORMAL", "<td width='100' align='center'>");
define("TD_JPEG", "<td width='100' align='center'>");
define("TD_SCORE", "<td width='50' align='center'>");


$PHP_SELF = $_SERVER['PHP_SELF'];

$cDatabase = new database();

$connect = $cDatabase->connectToDatabase();
$activeDatabase = $cDatabase->getActiveDatabase($connect);

$db = $cDatabase->selectDatabase($connect, $activeDatabase);
$selectArray[0] = '*';

$result = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, NULL, NULL, NULL, NULL);

$maxWeeks = $cDatabase->GetMaxWeeks($connect);

if($result)
{
	echo "<div align='center'>";
	echo "<table border='1'><thead><tr>";
	
	$finfo = mysqli_fetch_fields($result);
	for($i = 0; $i < mysqli_num_fields($result); $i++)
	{
		if($finfo[$i]->name == 'playerCode' || $finfo[$i]->name == 'playerName' || $finfo[$i]->name == 'playerTeam' || $finfo[$i]->name == 'playerValue' || $finfo[$i]->name == 'playerPosition')
		{
			switch ($finfo[$i]->name)
			{
			case 'playerCode'		: 	$heading = 'Code';
										break;
			case 'playerName'		: 	$heading = 'Name';
										break;
			case 'playerTeam'		: 	$heading = 'Team';
										break;
			case 'playerValue'		: 	$heading = 'Value';
										break;
			case 'playerPosition'	: 	$heading = 'Position';
										break;
			default					:	$heading = '';
										break;
			}
			$temp = $finfo[$i]->name;
			echo "<th>".$heading."";
			echo "</th>";
		}
	}
	
	$temp = 'playerScore';
	echo "<th>Score";
	echo "</th>";
	echo "</tr></thead><tbody></form>";

	for ($i = 0; $i < mysqli_num_rows($result); $i++)
	{
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		$playerCode = $row['playerCode'];
		$playerScore = getPlayerScore($row, $maxWeeks);
		if ($row['playerActive'] == 1)
		{
			echo TD_NORMAL.$colour.$row['playerCode']."</font></td>";
			echo TD_MAINNAME.$colour.$row['playerName']."</font></td>";
			echo TD_NORMAL.$colour.$row['playerTeam']."</font></td>";
			echo TD_NORMAL.$colour.$row['playerValue']."</font></td>";
			echo TD_NORMAL.$colour.$row['playerPosition']."</font></td>";
			echo TD_NORMAL.RED.$playerScore."</font></td></tr>";
		}		
	}
	echo "</tbody>
	</table></div>";
}


function getPlayerScore($row, $maxWeeks)
{
	$playerScore = 0;
			
	for ($j = 1; $j <= $maxWeeks; $j++)
	{
		$week = getWeekVariable($j);
		$playerScore += $row[$week];
	}
	
	return $playerScore;
}

echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>